#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/fs.h>

static int major = 0;
static int minor = 0;
module_param(major,int, S_IRUGO);
module_param(minor,int, S_IRUGO);
dev_t dev;

static int module_param_init(void)
{
    int ret;
    if (major) {
        printk("major: %d\n", major);
        printk("minor: %d\n", minor);
        dev = MKDEV(major, minor);
        ret = register_chrdev_region(dev, 1, "register_chrdev_num");
        if (ret) {
            printk("register_chrdev_region success!\n");
        } else {
            printk("register_chrdev_region fail!\n");   
        }
    } else {
        ret = alloc_chrdev_region(&dev,0, 1, "alloc_chrdev_num");
        if (ret) {
            printk("alloc_chrdev_region success!\n");
            major = MAJOR(dev);
            minor = MINOR(dev);
            printk("major: %d, minor: %d\n", major, minor);
        } else {
            printk("alloc_chrdev_region fail!\n");
        }
    }
    
    return 0;
}

static void module_param_exit(void)
{
    printk("helloworld exit!\n");
}

module_init(module_param_init);
module_exit(module_param_exit);
// 遵守GPL协议
MODULE_LICENSE("GPL");

MODULE_AUTHOR("wangzhinan");
MODULE_VERSION("V1.0");